package com.wangyadong.hobby.schedule.jobtask.javathread3.chapter2;

import com.wangyadong.hobby.thread.SheduledThreadPoolExecutorTest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.IntStream;

public class FunctionDemo {

    public static void main(String[] args) throws InterruptedException {
        int mThreads = 5;
        int cThreads = 1;
        ThreadPoolExecutor executorService = new MyThreadPoolExecutor(cThreads, mThreads,
                0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(5), new NamedThreadFactory("myThreadPool", false));
        sumbit(10, executorService);
        int poolSize = executorService.getPoolSize();
        System.out.println(poolSize);
        executorService.shutdown();
        boolean awaitTermination = executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
        //返回false  说明超时了
        if (!awaitTermination) {
            executorService.shutdownNow();
        } else {
            //返回true 说明线程已经关闭了
        }
        System.out.println(executorService.isTerminated());
//        executorService.remove()


    }

    private static void sumbit(int nThread, ThreadPoolExecutor executorService) {
        List<Future> list = new ArrayList<>();
        IntStream.range(0, nThread).forEach(i -> {
            //有返回值的Callable
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                }
            };
            String result = new String();
//            FutureTask<String> stringFutureTask = new FutureTask<>(runnable, result);
            Future<?> submit = executorService.submit(runnable);
            list.add(submit);
        });
        for (Future future : list) {
            try {
                Object o = future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }

}
